﻿<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeBehind="UserProfile.aspx.cs" Inherits="DFT.UserProfile" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <table width="100%">
        <tr>
            <td align="center">
                <table width="300px" cellpadding="2" cellspacing="2">
                    <tr>
                        <td align="left">
                            الاسم الكامل
                        </td>
                        <td align="right">
                            <input type="text" class="text ui-widget-content ui-corner-all" id="fullName" />
                        </td>
                    </tr>
                    <tr>
                        <td align="left">
                            البريد الالكتروني
                        </td>
                        <td align="right">
                            <input type="text" class="text ui-widget-content ui-corner-all" id="email" />
                        </td>
                    </tr>
                    <tr>
                        <td align="center" colspan="2">
                            <p id="dataMsgFail" class="validateTips" />
                        </td>
                    </tr>
                    <tr>
                        <td align="center" colspan="2">
                            <input type="button" value="تحديث البيانات" class="button gray" onclick="ChangeData();" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                        </td>
                    </tr>
                    <tr>
                        <td align="left">
                            كلمة المرور القديمة
                        </td>
                        <td align="right">
                            <input type="password" class="text ui-widget-content ui-corner-all" id="oldPass" />
                        </td>
                    </tr>
                    <tr>
                        <td align="left">
                            كلمة المرور الجديدة
                        </td>
                        <td align="right">
                            <input type="password" class="text ui-widget-content ui-corner-all" id="newPass" />
                        </td>
                    </tr>
                    <tr>
                        <td align="left">
                            تأكيد كلمة المرور
                        </td>
                        <td align="right">
                            <input type="password" class="text ui-widget-content ui-corner-all" id="reNewPass" />
                        </td>
                    </tr>
                    <tr>
                        <td align="center" colspan="2">
                            <p id="passMsgFail" class="validateTips" />
                        </td>
                    </tr>
                    <tr>
                        <td align="center" colspan="2">
                            <input type="button" value="تغيير كلمة المرور" class="button gray" onclick="ChangePass();" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    <script language="javascript" type="text/javascript">
        $("#fullName").val('<%=fullName%>');
        $("#email").val('<%=email%>');
        function ChangeData() {
            var newFullName = $("#fullName"),
                newEmail = $("#email"),
                allFields = $([]).add(newFullName).add(newEmail),
                tips = $("#dataMsgFail");

            var bValid = true;
            allFields.removeClass("ui-state-error");
            tips.text("").removeClass("ui-state-error");

            bValid = bValid && checkRequired(newFullName, tips, "يجب ادخال الاسم الكامل");
            bValid = bValid && checkRequired(newEmail, tips, "يجب إدخال البريد الالكتروني");
            bValid = bValid && checkRegexp(newEmail, tips, "eg. ui@jquery.com", /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);

            if (bValid) {
                UpdateUserDataRequest(newFullName.val(), newEmail.val())
            }
        }

        function UpdateUserDataRequest(newFullName, newEmail) {
            $.ajax({
                type: "POST",
                url: "AJAX/UpdateUserData.aspx",
                data: "newFullName=" + newFullName + "&newEmail=" + newEmail,
                cache: false,
                async: false,
                success: UpdateUserDataRespons
            });
        }

        function UpdateUserDataRespons(msg) {
            if (msg == "0") {
                updateTips($("#dataMsgFail"), "تم تحديث البيانات بنجاح");
            }
            else if (msg == "1") {
                $("#email").addClass("ui-state-error");
                updateTips($("#dataMsgFail"), "هذا البريد موجود مسبقا يرجى التحقق");
            }
            else {
                updateTips($("#dataMsgFail"), msg);
            }
        }


        function ChangePass() {
            var oldPassword = $("#oldPass"),
                newPassword = $("#newPass"),
                reNewPassword = $("#reNewPass"),
                allFields = $([]).add(oldPassword).add(newPassword).add(reNewPassword),
                tips = $("#passMsgFail");

            var bValid = true;
            allFields.removeClass("ui-state-error");
            tips.text("").removeClass("ui-state-error");

            bValid = bValid && checkRequired(oldPassword, tips, "يجب ادخال كلمة المرور الحالية");
            bValid = bValid && checkRequired(newPassword, tips, "يجب ادخال كلمة المرور الجديدة");
            bValid = bValid && checkRequired(reNewPassword, tips, "يجب ادخال تأكيد كلمة المرور الجديدة");
            bValid = bValid && checkEqual(reNewPassword, newPassword, tips, "كلمة المرور غير متطابقة");

            if (bValid) {
                ChangePasswordRequest(oldPassword.val(), newPassword.val());
                allFields.val("");
            }
        }

        function ChangePasswordRequest(oldPassword, newPassword) {
            $.ajax({
                type: "POST",
                url: "AJAX/ChangePasswordRequest.aspx",
                data: "oldPassword=" + oldPassword + "&newPassword=" + newPassword,
                cache: false,
                async: false,
                success: ChangePasswordRespons
            });
        }

        function ChangePasswordRespons(msg) {
            if (msg == "0") {
                updateTips($("#passMsgFail"), "تم تحديث كلمة المرور بنجاح");
            }
            else if (msg == "1") {
                $("#oldPass").addClass("ui-state-error");
                updateTips($("#passMsgFail"), "كلمة المرور الحالية غير صحيحة يرجى التحقق");
            }
            else {
                updateTips($("#passMsgFail"), msg);
            }
        }
    </script>
</asp:Content>
